package com.example.crm.mapper;

import com.example.crm.entity.User;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 用户接口
 * @author lenovo
 */
public interface UserMapper {
    /**
     * 添加用户
     * @param user
     */
    @Insert("insert into sys_user (usr_name,usr_password,usr_role_id,usr_flag)" +
            " values(#{usrName},#{usrPassword},#{usrRoleId},#{usrFlag})")
    public void insert(User user);

    /**
     * 删除用户
     * @param id
     */
    @Delete("delete from sys_user where usr_id=#{id}")
    public void delete(Long id);

    /**
     * 修改用户
     * @param user
     */
    @Update("<script>" + "update sys_user" + "<set>" +
            "<if test=\"usrName!=null\">usr_name = #{usrName},</if>"+
            "<if test=\"usrPassword!=null\">usr_password = #{usrPassword},</if>"+
            "<if test=\"usrRoleId!=null\">usr_role_id = #{usrRoleId},</if>"+
            "<if test=\"usrFlag!=null\">usr_flag = #{usrFlag},</if>"+
            "</set>"+"where usr_id = #{usrId}"+"</script>")
    public void update(User user);

    /**
     * 根据id查询用户信息
     * @param id
     * @return
     */
    @Select("select usr_id,usr_name,usr_password,usr_role_id,usr_flag"+"from sys_user where usr_id=#{id}")
    @Results({@Result(column = "usr_id",property = "usrId"),@Result(column = "usr_name",property = "usrName"),
            @Result(column = "usr_role_id",property = "usrRoleId"),@Result(column = "usr_password",property = "usrPassword"),
            @Result(column = "usr_flag",property = "usrFlag")})
    public User get(Long id);

    /**
     * 查询信息
     * @return
     */
    @Select("select usr_id,usr_name,usr_password,usr_role_id,usr_flag from sys_user")
    @Results({@Result(column = "usr_id",property = "usrId"),@Result(column = "usr_name",property = "usrName"),
            @Result(column = "usr_role_id",property = "usrRoleId"),@Result(column = "usr_password",property = "usrPassword"),
            @Result(column = "usr_flag",property = "usrFlag")})
    public List<User> findAll();
}